#ifndef _BACKUP_IMAGE_H_
#define _BACKUP_IMAGE_H_
#include "sys_hal_types.h"
#include <stddef.h>
#define CFG_UPDATE_IMAGE                     1
#define UART_PRINTF                          1
#define BOOT_UART_DEBUG                                          0
#define CFG_UDI_ERASE_ALL_FIRST                            1
#define FLASH_PAGE                                        (256)
#define FLASH_PAGE_MASK                                   (FLASH_PAGE - 1)
#define FLASH_ERASED_CODE                                 (0xFFFFFFFF)
#define FLASH_ERASE_SECTOR_SIZE_RSL_BIT_CNT               (12)
#define FLASH_ERASE_SECTOR_SIZE                           (4096)
#define FLASH_ERASE_SECTOR_SIZE_MASK                      (FLASH_ERASE_SECTOR_SIZE - 1)
#define UPDATE_CHUNK_SIZE                                 (32)
typedef void (*FUNCPTR)(void);
#define MAX(x, y)                  (((x) > (y)) ? (x) : (y))
#define MIN(x, y)                  (((x) < (y)) ? (x) : (y))
#define max(x, y)                  (((x) > (y)) ? (x) : (y))
#define min(x, y)                  (((x) < (y)) ? (x) : (y))
#define UDI_DEFAULT_VERSION                 (0x11110000)
#define UDI_SIGNATURE_CODE                  (0x88776655)
#define ENCRYPT_FRAME_SIZE             (0x20)
#define ENCRYPT_CRC_SIZE               (0x02)
#define crc_size(len)                  ((len >> 5) * (ENCRYPT_FRAME_SIZE + ENCRYPT_CRC_SIZE))
#define crc_address(len)               (((unsigned int)(len >> 5)) * (ENCRYPT_FRAME_SIZE + ENCRYPT_CRC_SIZE) + (len & (ENCRYPT_FRAME_SIZE - 1)))
typedef struct
{
    unsigned int signature;          /* section signature*/
    unsigned int version;            /* section version*/
    unsigned int sec_size;
    unsigned int rsv[5];             /* for scatter file adds the attribute word: ALIGNALL 32*/
} SECTION_BOUNDARY_T, *SECTION_BOUNDARY_PTR;
enum
{
    SSTATUS_UNKOWN           = 0,
    SSTATUS_SECT_ERASED,
    SSTATUS_UPDATE_NORMAL,
    SSTATUS_UPDATE_ABNORMAL,
};
typedef struct
{
    SECTION_BOUNDARY_T left_boundary;
    SECTION_BOUNDARY_T right_boundary;
    u_int32 sect_status;
    u_int32 erase_offset;
    u_int32 update_offset;
} UDI_SECTION_T, *UDI_SECTION_PTR;


#ifdef CFG_UPDATE_IMAGE
extern UDI_SECTION_T psec_A;
extern UDI_SECTION_T psec_B;
#ifdef OAD_IMAGE_A
extern UDI_SECTION_T bsec_B;
#endif
#ifdef OAD_IMAGE_B
extern UDI_SECTION_T bsec_A;
#endif 
#endif  
#if CFG_UPDATE_IMAGE
extern void *udi_memcpy(void *dest, const void *src, size_t count);
extern void * udi_memset32(void * dest, u_int32 value, size_t size);
extern void udi_delay_ms(unsigned int tt);
extern void udi_reset(void);
extern void udi_hex_printf(uint8 *buffer, uint8 len);
extern void fflash_rd_data(uint8 *buffer, uint32 address, uint32 len);
extern void fflash_wr_data (uint8 *buffer, uint32 address, uint32 len);
extern u_int32 udi_erase_section(u_int32 address, u_int32 len);
extern void fflash_erase_one_sector(uint32 address);
extern void udi_hex_int_print(int prf_val);
extern void fflash_initialization(void);
extern void udi_wdt_disable(void);
extern void udi_icu_init(void);
extern void udi_uart_init(void);
#endif // CFG_UPDATE_IMAGE
#endif // _BACKUP_IMAGE_H_
// eof
